﻿package com.miracle.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils
{
  public static boolean isNotEmpty(String str)
  {
    return (str != null) && (str.trim().length() > 0);
  }

  public static boolean isEmpty(String aStr)
  {
    return (aStr == null) || (aStr.trim().length() == 0);
  }

  public static String left(String aStr, int aLen)
  {
    if (aLen < 0) {
      throw new IllegalArgumentException("Requested String length " + aLen + " is less than zero");
    }
    if ((aStr == null) || (aStr.length() <= aLen)) {
      return aStr;
    }
    return aStr.substring(0, aLen);
  }

  public static String right(String aStr, int aLen)
  {
    if (aLen < 0) {
      throw new IllegalArgumentException("Requested String length " + aLen + " is less than zero");
    }
    if ((aStr == null) || (aStr.length() <= aLen)) {
      return aStr;
    }
    return aStr.substring(aStr.length() - aLen);
  }

  public static String repeat(String aStr, int aRepeat)
  {
    StringBuffer tBuffer = new StringBuffer(aRepeat * aStr.length());
    for (int i = 0; i < aRepeat; i++) {
      tBuffer.append(aStr);
    }
    return tBuffer.toString();
  }

  public static String rightPad(String aStr, int aSize)
  {
    return rightPad(aStr, aSize, " ");
  }

  public static String rightPad(String aStr, int aSize, String aDelim)
  {
    aSize = (aSize - aStr.length()) / aDelim.length();
    if (aSize > 0) {
      aStr = aStr + repeat(aDelim, aSize);
    }
    return aStr;
  }

  public static String leftPad(String aStr, int aSize)
  {
    return leftPad(aStr, aSize, " ");
  }

  public static String leftPad(String aStr, int aSize, String aDelim)
  {
    aSize = (aSize - aStr.length()) / aDelim.length();
    if (aSize > 0) {
      aStr = repeat(aDelim, aSize) + aStr;
    }
    return aStr;
  }

  public static boolean isAlpha(String aStr)
  {
    if (aStr == null) {
      return false;
    }
    int tSz = aStr.length();
    for (int i = 0; i < tSz; i++) {
      if (!Character.isLetter(aStr.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static boolean isAlphaSpace(String aStr)
  {
    if (aStr == null) {
      return false;
    }
    int tSz = aStr.length();
    for (int i = 0; i < tSz; i++) {
      if ((!Character.isLetter(aStr.charAt(i))) && (aStr.charAt(i) != ' ')) {
        return false;
      }
    }
    return true;
  }

  public static boolean isAlphanumeric(String aStr)
  {
    if (aStr == null) {
      return false;
    }
    int tSz = aStr.length();
    for (int i = 0; i < tSz; i++) {
      if (!Character.isLetterOrDigit(aStr.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static boolean isAlphanumericSpace(String aStr)
  {
    if (aStr == null) {
      return false;
    }
    int tSz = aStr.length();
    for (int i = 0; i < tSz; i++) {
      if ((!Character.isLetterOrDigit(aStr.charAt(i))) && (aStr.charAt(i) != ' ')) {
        return false;
      }
    }
    return true;
  }

  public static boolean isNumeric(String aStr)
  {
    if (aStr == null) {
      return false;
    }
    int tSz = aStr.length();
    for (int i = 0; i < tSz; i++) {
      if (!Character.isDigit(aStr.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static boolean isNumericSpace(String aStr)
  {
    if (aStr == null) {
      return false;
    }
    int tSz = aStr.length();
    for (int i = 0; i < tSz; i++) {
      if ((!Character.isDigit(aStr.charAt(i))) && (aStr.charAt(i) != ' ')) {
        return false;
      }
    }
    return true;
  }

  public static boolean validateNum(String num)
  {
    boolean isMatch = false;

    Pattern tMask = Pattern.compile("^[0-9]*[1-9][0-9]*$");
    if (isNotEmpty(num))
    {
      Matcher tMatch = tMask.matcher(num);
      if (tMatch.matches()) {
        isMatch = true;
      }
    }

    return isMatch;
  }

  public static String fen2yuanScale2(String price) {
	  BigDecimal decimal = new BigDecimal(price);
	  decimal = decimal.divide(new BigDecimal("100")).setScale(2);
	  return decimal.toString();
  }
  
  public static boolean isNumber(String number, int decDegit)
  {
    if (isEmpty(number)) return false;
    number = number.trim();

    int dot = number.indexOf(".");
    if ((dot != -1) && (decDegit == 0)) return false;
    if (number.lastIndexOf(".") != dot) return false;
    if (dot == 0) return false;
    String intNumber = dot == -1 ? number : number.substring(0, dot);
    String decNumber = dot == -1 ? "" : number.substring(dot + 1);
    if (decNumber.length() > decDegit) return false;
    if (intNumber.startsWith("00")) return false;
    for (int i = 0; i < intNumber.length(); i++) {
      char ch = intNumber.charAt(i);
      if ((ch < '0') || (ch > '9')) return false;
    }

    for (int i = 0; i < decNumber.length(); i++) {
      char ch = decNumber.charAt(i);
      if ((ch < '0') || (ch > '9')) return false;
    }
    return true;
  }
}